package br.com.jokim.didaque.ui.activity;

import roboguice.activity.RoboFragmentActivity;
import roboguice.inject.ContentView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.RadioButton;
import android.widget.ViewFlipper;
import br.com.jokim.didaque.R;

@ContentView(R.layout.activity_flipper)
public class FlipperActivity extends RoboFragmentActivity {

	RadioButton RB0;
	RadioButton RB1;
	RadioButton RB2;
	
	
	ViewFlipper VF;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/*
		 * Find the views declared in main.xml.
		 */
		RB0 = (RadioButton) findViewById(R.id.radio0);
		RB1 = (RadioButton) findViewById(R.id.radio1);
		RB2 = (RadioButton) findViewById(R.id.radio2);
		VF = (ViewFlipper) findViewById(R.id.ViewFlipper01);

		/*
		 * Set a listener that will listen for clicks on the radio buttons and
		 * perform suitable actions.
		 */
		RB0.setOnClickListener(radio_listener);
		RB1.setOnClickListener(radio_listener);
		RB2.setOnClickListener(radio_listener);
	}

	/*
	 * Define a OnClickListener that will change which view that is displayed by
	 * the ViewFlipper
	 */
	private OnClickListener radio_listener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.radio0:
				VF.setInAnimation(inFromRightAnimation());
				VF.setOutAnimation(outToLeftAnimation());
				VF.setDisplayedChild(0);      				
				break;
			case R.id.radio1:
				VF.setInAnimation(inFromLeftAnimation());
		        VF.setOutAnimation(outToRightAnimation());
				VF.setDisplayedChild(1);
				break;
			case R.id.radio2:
				VF.setDisplayedChild(2);
				break;
			}
		}
	};

	private Animation inFromRightAnimation() {

		Animation inFromRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(500);
		inFromRight.setInterpolator(new AccelerateInterpolator());
		return inFromRight;
	}

	private Animation outToLeftAnimation() {
		Animation outtoLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(500);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		return outtoLeft;
	}

	private Animation inFromLeftAnimation() {
		Animation inFromLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(500);
		inFromLeft.setInterpolator(new AccelerateInterpolator());
		return inFromLeft;
	}

	private Animation outToRightAnimation() {
		Animation outtoRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoRight.setDuration(500);
		outtoRight.setInterpolator(new AccelerateInterpolator());
		return outtoRight;
	}

}
